package com.egao.api.controller.orderB;


import com.egao.api.base.BaseApiController;
import com.egao.common.core.utils.messages.PhoneUtils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageResult2;
import com.egao.common.system.entity.OperRecord;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * https://francjc.com:4332/swagger-ui.html
 *
 */
@RequestMapping("/api/customer")
@RestController
@Api(value = "客户",tags = {"客户"})
public class ApiCustomerlController extends BaseApiController {

    @Autowired
    private OperRecordService operRecordService;
    @Autowired
    private CustomerService customerService;

    @PostMapping("/list")
    @ApiOperation(value = "客户列表",notes = "limit:每页显示几行 必填\n" +
            "page:第几页 必填\n" +
            "name  客户名称\n" +
            "state  状态:未审核/已审核\n" +
            "salesman  业务员(我们公司的)\n" +
            "settle  周结,月结\n\n\n\n\n\n" +
            "返回的数据:id int id\n" +
            "companyId varchar 公司id\n" +
            "name varchar 客户名称\n" +
            "adminUser varchar 管理员帐号\n" +
            "code varchar 客户代码\n" +
            "socialCode varchar 统一社会信用代码\n" +
            "businessLicense varchar 营业执照图片\n" +
            "salesman varchar 业务员(我们公司的)\n" +
            "settle varchar 结算周期(提醒用的)周结,月结\n" +
            "contacts varchar 联系人\n" +
            "contactsPhone varchar 联系人电话\n" +
            "address varchar 公司店址\n" +
            "state varchar 审核状态: 未审核,已审核\n" +
            "orderNum int 排序\n" +
            "createTime datetime 创建时间\n" +
            "createUser varchar 创建人\n" +
            "updateTime datetime 更新时间\n" +
            "updateUser varchar 更新人\n" +
            "deleted int del(0正常 1删除)",httpMethod = "POST")
    @ResponseBody
    public JSONObject list(PageResult2<T> page, Customer customer, HttpServletRequest request)  throws Exception{
        return customerService.list(page, customer, getLoginUser(request));
    }
    @PostMapping("/listAll")
    @ApiOperation(value = "客户列表",notes = "" +
            "name  客户名称\n" +
            "state  状态:未审核/已审核\n" +
            "salesman  业务员(我们公司的)\n" +
            "settle  周结,月结\n\n\n\n\n\n" +
            "返回的数据:id int id\n" +
            "companyId varchar 公司id\n" +
            "name varchar 客户名称\n" +
            "adminUser varchar 管理员帐号\n" +
            "code varchar 客户代码\n" +
            "socialCode varchar 统一社会信用代码\n" +
            "businessLicense varchar 营业执照图片\n" +
            "salesman varchar 业务员(我们公司的)\n" +
            "settle varchar 结算周期(提醒用的)周结,月结\n" +
            "contacts varchar 联系人\n" +
            "contactsPhone varchar 联系人电话\n" +
            "address varchar 公司店址\n" +
            "state varchar 审核状态: 未审核,已审核\n" +
            "orderNum int 排序\n" +
            "createTime datetime 创建时间\n" +
            "createUser varchar 创建人\n" +
            "updateTime datetime 更新时间\n" +
            "updateUser varchar 更新人\n" +
            "deleted int del(0正常 1删除)",httpMethod = "POST")
    @ResponseBody
    public JsonResult listAll(Customer customer, HttpServletRequest request)  throws Exception{
        return JsonResult.ok().setData(customerService.list(customer, getLoginUser(request)));
    }

    @PostMapping("/getById")
    @ApiOperation(value = "客户查询",notes = "id ",httpMethod = "POST")
    @ResponseBody
    public JsonResult getById(Customer customer, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户查询", "客户查询",
                request.getServletPath(), "POST", "ApiCustomerController.getById", request.getQueryString(),//JSONObject.toJSON(customer).toString(),
                "", getIpAddress(request)));
        return JsonResult.ok().setData(customerService.getById(customer));
    }

    @PostMapping("/save")
    @ApiOperation(value = "客户添加",notes = "name varchar 客户名称\n" +
            "code varchar 统一社会信用代码\n" +
            "business_license varchar 营业执照图片地址\n" +
            "salesman varchar 业务员(我们公司的)\n" +
            "contacts varchar 联系人\n" +
            "contactsPhone varchar 联系人电话\n" +
            "address varchar 公司店址\n" +
            "settle varchar 结算周期(提醒用的)周结,月结 ",httpMethod = "POST")
    @ResponseBody
    public JsonResult save(Customer customer, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户添加", "客户添加",
                request.getServletPath(), "POST", "ApiCustomerController.save", request.getQueryString(),//JSONObject.toJSON(customer).toString(),
                "", getIpAddress(request)));
        customer.setState("为审核");
        return customerService.save(customer, getLoginUser(request));
    }

    @PostMapping("/update")
    @ApiOperation(value = "客户修改",notes = "" +
            "id id 必填\n" +
            "code varchar 公司代码\n" +
            "salesman varchar 业务员(我们公司的)\n" +
            "settle varchar 结算周期(提醒用的)周结,月结\n" +
            "contacts varchar 联系人\n" +
            "contactsPhone varchar 联系人电话\n" +
            "address varchar 公司地址\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult update(Customer customer, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户修改", "客户修改",
                request.getServletPath(), "POST", "ApiCustomerController.update", request.getQueryString(),//JSONObject.toJSON(customer).toString(),
                "", getIpAddress(request)));
        return customerService.update(customer, getLoginUser(request));
    }

    @PostMapping("/examine")
    @ApiOperation(value = "客户审核通过",notes = "id id",httpMethod = "POST")
    @ResponseBody
    public JsonResult examineSuccess(Customer customer, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户审核通过", "客户审核通过",
                request.getServletPath(), "POST", "ApiCustomerController.examine", request.getQueryString(),//JSONObject.toJSON(customer).toString(),
                "", getIpAddress(request)));

        return customerService.examine(customer, getLoginUser(request));
    }


    @PostMapping("/examineFail")
    @ApiOperation(value = "客户审核未通过",notes = "id id",httpMethod = "POST")
    @ResponseBody
    public JsonResult examineFail(Customer customer, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户审核未通过", "客户审核未通过",
                request.getServletPath(), "POST", "ApiCustomerController.examineFail", request.getQueryString(),//JSONObject.toJSON(customer).toString(),
                "", getIpAddress(request)));
        customer.setState("未通过");
        return customerService.examineFail(customer, getLoginUser(request));
    }

}
